package com.infotel.cartegrise.ihm;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.infotel.cartegrise.listeners.MenuItemQuitterListener;

public class IHMFinale extends JFrame {

    private static final long serialVersionUID = 1L;

    private PanneauEnregistrer panneauEnregistrer;
    private PanneauExporter panneauExporter;
    private PanneauAbout panneauAbout;
    private JTabbedPane onglets;

    private JMenuBar barreMenu;
    private JMenu menuFichier;
    private JMenuItem itemQuitter;
    private JMenuItem itemEnregistrer;
    private JMenuItem itemExporter;

    public static String sFichier = "Fichier";
    public static String sQuitter = "Quitter";
    public static String sEnregistrer = "Enregistrer";
    public static String sExporter = "Exporter";
    public static String sAbout = "A Propos";

    private static Properties globalProperties;

    public IHMFinale() {

        IHMFinale.globalProperties = new Properties();
        try {
            IHMFinale.globalProperties.load(new FileInputStream(
                    "ressources/config.properties"));
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        panneauEnregistrer = new PanneauEnregistrer();
        panneauExporter = new PanneauExporter();
        panneauAbout = new PanneauAbout();
        onglets = new JTabbedPane();
        onglets.addTab(sEnregistrer, panneauEnregistrer);
        onglets.addTab(sExporter, panneauExporter);
        onglets.addTab(sAbout, panneauAbout);

        this.setLayout(new BorderLayout(1, 10));
        this.add(onglets, BorderLayout.CENTER);

        barreMenu = new JMenuBar();
        menuFichier = new JMenu(sFichier);
        barreMenu.add(menuFichier);

        itemQuitter = new JMenuItem(sQuitter);
        itemEnregistrer = new JMenuItem(sEnregistrer);
        itemExporter = new JMenuItem(sExporter);

        menuFichier.add(itemEnregistrer);
        menuFichier.add(itemExporter);
        menuFichier.add(itemQuitter);

        itemQuitter.addActionListener(new MenuItemQuitterListener());
        itemEnregistrer.addActionListener(new MenuItemEnregistrerListener());

        this.setJMenuBar(barreMenu);
        this.setMinimumSize(new Dimension(
                Integer.parseInt(IHMFinale.globalProperties
                        .getProperty("windowWidth")), Integer
                        .parseInt(IHMFinale.globalProperties
                                .getProperty("windowHeight"))));

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        this.setLocation(dim.width / 2 - this.getWidth() / 2, dim.height / 2
                - this.getHeight() / 2);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static Properties getGlobalProperties() {
        return globalProperties;
    }

    public void setGlobalProperties(Properties globalProperties) {
        IHMFinale.globalProperties = globalProperties;
    }
}
